/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package db

import "math"

type Page struct {
	PageNum  int         `json:"pageNum"`
	PageSize int         `json:"pageSize"`
	Total    int64       `json:"totalCount"`
	Pages    int         `json:"pages"`
	List     interface{} `json:"list"`
}

func NewPage(total int64, pageNum int) *Page {
	return &Page{
		PageNum:  pageNum,
		PageSize: 10,
		Total:    total,
		Pages:    0,
	}
}

func (p *Page) SetPageSize(pageSize int) *Page {
	if pageSize == 0 {
		pageSize = 10
	}
	p.PageSize = pageSize
	return p
}

func (p *Page) SetTotal(total int64) *Page {
	p.Total = total
	return p
}

func (p *Page) SetPageNo(pageNum int) *Page {
	p.PageNum = pageNum
	return p
}

func (p *Page) CreatePage(data interface{}) *Page {
	p.Pages = int(math.Ceil(float64(p.Total) / float64(p.PageSize)))
	p.List = data
	return p
}
